package com.memogame;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.sax.TextElementListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class OptionsActivity extends Activity {

	private static String METHOD_NAME_1 = "CalculateSumTestMethod";
	private static String SOAP_ACTION_1 = Data.NAMESPACE + METHOD_NAME_1;

	private static String METHOD_NAME_2 = "GetOptions";
	private static String SOAP_ACTION_2 = Data.NAMESPACE + METHOD_NAME_2;

	private static String METHOD_NAME_3 = "SetOptions";
	private static String SOAP_ACTION_3 = Data.NAMESPACE + METHOD_NAME_3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AsyncCallGetOptionsWS opt = new AsyncCallGetOptionsWS();
		opt.execute();
		
		setContentView(R.layout.activity_options);

		EditText ipAddress = (EditText) findViewById(R.id.ipEditText);
		ipAddress.setText(Data.IP_ADDRESS, TextView.BufferType.EDITABLE);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_options, menu);
		return true;
	}

	public void onTestConnectionClicked(View v) {
		//Log.i(Data.TAG, "z1 onTestConnectionClicked");
		EditText ipEdit = (EditText) findViewById(R.id.ipEditText);
		String ip = ipEdit.getText().toString();

		TextView tv = (TextView) findViewById(R.id.ipInfoTextView);
		tv.setText("Checking connection...");

		AsyncCallTestWS test = new AsyncCallTestWS(ip);
		test.execute();
	}

	public void onSaveIPClicked(View v) {

		EditText ipEdit = (EditText) findViewById(R.id.ipEditText);
		Data.URL = ipEdit.getText().toString();
		Log.i(Data.TAG, "z1 URL=" + Data.URL);
		
	}

	public void onSaveOptionsClicked(View v) {
		//Log.i(Data.TAG, "z1 onSaveOptionsClicked");
		
		EditText p = (EditText) findViewById(R.id.playerNameEditText);
		String name = p.getText().toString();
		
		EditText b = (EditText) findViewById(R.id.boardSizeEditText);
		int size = Integer.parseInt(b.getText().toString());
			
		TextView tv = (TextView) findViewById(R.id.ipInfoTextView);
		tv.setText("Sending...");
		
		//Log.i(Data.TAG, "z1 name: " + name + " size: " + size);
		AsyncCallSetOptionsWS set = new AsyncCallSetOptionsWS(name, size);
		set.execute();
	}

	private class AsyncCallTestWS extends AsyncTask<Void, Void, Void> {

		int result;
		String ip;

		public AsyncCallTestWS(String ip) {
			this.ip = ip;
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			this.result = getTestConnectionResult(this.ip);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			TextView tv = (TextView) findViewById(R.id.ipInfoTextView);
			if (this.result == 7) {
				tv.setTextColor(Color.GREEN);
				tv.setText("Connection passed.");
			} else {
				tv.setTextColor(Color.RED);
				tv.setText("Connection failed!");
			}
		}

	}

	private int getTestConnectionResult(String ip) {

		String url = "http://" + ip + "/MemoGameService.asmx";
		SoapObject Request = new SoapObject(Data.NAMESPACE, METHOD_NAME_1);

		int result = 0;
		PropertyInfo pi = new PropertyInfo();
		pi.setName("a");
		pi.setValue(2);
		pi.setType(int.class);
		Request.addProperty(pi);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("b");
		pi2.setValue(5);
		pi2.setType(int.class);
		Request.addProperty(pi2);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(Request);

		try {
			AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
					url);

			androidHttpTransport.call(SOAP_ACTION_1, envelope);
			result = Integer.parseInt(envelope.getResponse().toString());
			Log.i(Data.TAG, "z1 result: " + result);

		} catch (Exception e) {
			Log.e(Data.TAG, "z1 error: " + e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
	
	private class AsyncCallGetOptionsWS extends AsyncTask<Void, Void, Void> {

		String playerName = "";
		int boardSize = 0;
		
		@Override
		protected Void doInBackground(Void... arg0) {
			
			SoapObject Request = new SoapObject(Data.NAMESPACE, METHOD_NAME_2);

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.dotNet = true;
			envelope.setOutputSoapObject(Request);

			try {
				AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
						Data.URL);

				androidHttpTransport.call(SOAP_ACTION_2, envelope);
				SoapObject result = (SoapObject) envelope.bodyIn;
				
				SoapObject root = (SoapObject) result.getProperty(0);		
				SoapObject root1 = (SoapObject) root.getProperty(0);
				
				playerName = root1.getProperty("person_name").toString();
				boardSize = Integer.parseInt(root1.getProperty("board_size").toString());
							
			} catch (Exception e) {
				Log.e(Data.TAG, "z1 error-getOption: " + e.getMessage());
				e.printStackTrace();
			}

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			Log.i(Data.TAG, "z1 " + this.playerName + "," + this.boardSize);
			EditText player1 = (EditText) findViewById(R.id.playerNameEditText);
			player1.setText(playerName, TextView.BufferType.EDITABLE);
			
			EditText board = (EditText) findViewById(R.id.boardSizeEditText);
			board.setText(String.valueOf(boardSize), TextView.BufferType.EDITABLE);	
		}

	}
	
	private class AsyncCallSetOptionsWS extends AsyncTask<Void, Void, Void> {

		boolean result;
		String playerName;
		int boardSize;
		public AsyncCallSetOptionsWS(String playerName, int boardSize) {
			this.playerName = playerName;
			this.boardSize = boardSize;
		}
		
		@Override
		protected Void doInBackground(Void... arg0) {
			SoapObject Request = new SoapObject(Data.NAMESPACE, METHOD_NAME_3);
			
			PropertyInfo pi = new PropertyInfo();
			pi.setName("playerName");
			pi.setValue(this.playerName);
			pi.setType(String.class);
			Request.addProperty(pi);

			PropertyInfo pi2 = new PropertyInfo();
			pi2.setName("boardSize");
			pi2.setValue(this.boardSize);
			pi2.setType(int.class);
			Request.addProperty(pi2);

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.dotNet = true;
			envelope.setOutputSoapObject(Request);

			try {
				AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
						Data.URL);

				androidHttpTransport.call(SOAP_ACTION_3, envelope);
				this.result = Boolean.valueOf(envelope.getResponse().toString());
				Log.i(Data.TAG, "z1 result: " + this.result);

			} catch (Exception e) {
				Log.e(Data.TAG, "z1 error-setOptions: " + e.getMessage());
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			TextView tv = (TextView) findViewById(R.id.ipInfoTextView);
			if (this.result) {
				tv.setTextColor(Color.GREEN);
				tv.setText("Options changed successfully.");
			} else {
				tv.setTextColor(Color.RED);
				tv.setText("Failure!");
			}
		}

	}


}
